{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyN0utP4LV00DyL1niqAAAei",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/DanielWarfield1/MLWritingAndResearch/blob/main/GroundX_Llama_3_1_70B.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# GroundX + Llama 3.1 70B"
      ],
      "metadata": {
        "id": "5oFfyg5TKyt9"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "cxRZ7aL4kdnW",
        "outputId": "7656adfe-c8b4-4546-c30e-fd7a67ae7a03"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Requirement already satisfied: unifyai in /usr/local/lib/python3.10/dist-packages (0.8.5)\n",
            "Requirement already satisfied: openai<2.0.0,>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from unifyai) (1.37.0)\n",
            "Requirement already satisfied: requests<3.0.0,>=2.31.0 in /usr/local/lib/python3.10/dist-packages (from unifyai) (2.31.0)\n",
            "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.12.0->unifyai) (3.7.1)\n",
            "Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai<2.0.0,>=1.12.0->unifyai) (1.7.0)\n",
            "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.12.0->unifyai) (0.27.0)\n",
            "Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.12.0->unifyai) (2.8.2)\n",
            "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.12.0->unifyai) (1.3.1)\n",
            "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.12.0->unifyai) (4.66.4)\n",
            "Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.12.0->unifyai) (4.12.2)\n",
            "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.31.0->unifyai) (3.3.2)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.31.0->unifyai) (3.7)\n",
            "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.31.0->unifyai) (2.0.7)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.31.0->unifyai) (2024.7.4)\n",
            "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.12.0->unifyai) (1.2.2)\n",
            "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.12.0->unifyai) (1.0.5)\n",
            "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.12.0->unifyai) (0.14.0)\n",
            "Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai<2.0.0,>=1.12.0->unifyai) (0.7.0)\n",
            "Requirement already satisfied: pydantic-core==2.20.1 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai<2.0.0,>=1.12.0->unifyai) (2.20.1)\n",
            "Requirement already satisfied: groundx-python-sdk in /usr/local/lib/python3.10/dist-packages (1.3.19)\n",
            "Requirement already satisfied: aiohttp<4.0.0,>=3.8.4 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (3.9.5)\n",
            "Requirement already satisfied: certifi>=2023.7.22 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (2024.7.4)\n",
            "Requirement already satisfied: cryptography<43.0.0,>=42.0.5 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (42.0.8)\n",
            "Requirement already satisfied: frozendict<3.0.0,>=2.3.4 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (2.4.4)\n",
            "Requirement already satisfied: python-dateutil<3.0.0,>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (2.8.2)\n",
            "Requirement already satisfied: typing_extensions<5.0.0,>=4.3.0 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (4.12.2)\n",
            "Requirement already satisfied: urllib3<3.0.0,>=1.26.18 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (2.0.7)\n",
            "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (1.3.1)\n",
            "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (23.2.0)\n",
            "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (1.4.1)\n",
            "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (6.0.5)\n",
            "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (1.9.4)\n",
            "Requirement already satisfied: async-timeout<5.0,>=4.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (4.0.3)\n",
            "Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.10/dist-packages (from cryptography<43.0.0,>=42.0.5->groundx-python-sdk) (1.16.0)\n",
            "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil<3.0.0,>=2.8.2->groundx-python-sdk) (1.16.0)\n",
            "Requirement already satisfied: pycparser in /usr/local/lib/python3.10/dist-packages (from cffi>=1.12->cryptography<43.0.0,>=42.0.5->groundx-python-sdk) (2.22)\n",
            "Requirement already satisfied: idna>=2.0 in /usr/local/lib/python3.10/dist-packages (from yarl<2.0,>=1.0->aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (3.7)\n"
          ]
        }
      ],
      "source": [
        "#for calling generative model\n",
        "!pip install unifyai\n",
        "\n",
        "#for performing advanced RAG\n",
        "!pip install groundx-python-sdk"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"\"\"Authenticating GroundX for advanced RAG and Unify\n",
        "\"\"\"\n",
        "\n",
        "from google.colab import userdata\n",
        "import os\n",
        "unify_api_key = userdata.get('xxxxxxxxxxxx')\n",
        "os.environ['UNIFY_KEY'] = unify_api_key\n",
        "\n",
        "from groundx import Groundx\n",
        "groundx = Groundx(\n",
        "    api_key=userdata.get('xxxxxxxxxx'),\n",
        ")"
      ],
      "metadata": {
        "id": "XtvItSZ_koJr"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Specifying Bucket\n",
        "# Follow this tutorial to upload content to your own bucket:\n",
        "# https://www.groundx.ai/post/groundx-in-5-minutes-building-a-legal-assistant\n",
        "bucketId = 7802"
      ],
      "metadata": {
        "id": "8ljOopIHlTdF"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def gx_retreive(query):\n",
        "    \"\"\"Getting GroundX context from docs based on query\n",
        "    \"\"\"\n",
        "    #getting GroundX Retreival\n",
        "    response = groundx.search.content(\n",
        "        id=bucketId,\n",
        "        query=query\n",
        "    )\n",
        "\n",
        "    #Getting GroundX Recommended Context\n",
        "    return response.body['search']['text']"
      ],
      "metadata": {
        "id": "6vUii3bxln_9"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def gx_retreive_and_augment(query):\n",
        "    \"\"\"Constructing an augmented prompt\n",
        "    \"\"\"\n",
        "\n",
        "    #getting context\n",
        "    context = gx_retreive(query)[:20000]\n",
        "\n",
        "    #defining a high level prompt so the LLM knows what to do\n",
        "    system_prompt = 'you are a helpful AI agent tasked with helping\\\n",
        "    users to extract information from the context below'\n",
        "\n",
        "    #based on OpenAI's new formatting\n",
        "    augmented_prompt = '\\n'.join([system_prompt+'\\n\\n===\\n'+context+'\\n===','query:',query])\n",
        "\n",
        "    return augmented_prompt"
      ],
      "metadata": {
        "id": "mSYTeke3lsvj"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from unify import Unify\n",
        "\n",
        "#defining model and endpoint\n",
        "unify = Unify(\"llama-3.1-70b-chat@together-ai\")\n",
        "\n",
        "#generating based on GroundX Augmented Prompt\n",
        "response = unify.generate(gx_retreive_and_augment('What is the branch rate in Belgium?'))\n",
        "response"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 37
        },
        "id": "RSkKrZvc5yzB",
        "outputId": "f573bb10-254c-487b-9041-2c0379f223f3"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'According to the first text excerpt, the \"Branch Tax Rate (%)\" in Belgium is a flat 25%.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 62
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "tLhDJWGsLKOY"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}